#include "Matrix4x4.h"

Matrix4x4 Matrix4x4::g_Identity(D3DXMATRIX(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1));

Matrix4x4::Matrix4x4(D3DXMATRIX &mat)
{
	memcpy(&m, &mat.m, sizeof(mat.m));
}

void Matrix4x4::SetPosition(Vec3 const &pos)
{
	m[3][0] = pos.x;
	m[3][1] = pos.y;
	m[3][2] = pos.z;
	m[3][3] = 1.0f;
}

void Matrix4x4::SetPosition(Vec4 const &pos)
{
	m[3][0] = pos.x;
	m[3][1] = pos.y;
	m[3][2] = pos.z;
	m[3][3] = pos.w;
}

Vec3 Matrix4x4::GetPosition() const
{
	return Vec3(m[3][0], m[3][1], m[3][2]);
}

Vec4 Matrix4x4::Xform(Vec4 &v) const
{
	Vec4 temp;
	D3DXVec4Transform(&temp, &v, this);
	return temp;
}

//gives the vector from matrix to the position
//with the matrix being at 0,0,0
Vec3 Matrix4x4::Xform(Vec3 &v) const
{
	Vec4 temp(v);
	Vec4 out;
	D3DXVec4Transform(&out, &temp, this);
	return Vec3(out.x, out.y, out.z);
}

Matrix4x4 Matrix4x4::Inverse() const
{
	Matrix4x4 out;
	D3DXMatrixInverse(&out, NULL, this);
	return out;
}

void Matrix4x4::BuildTranslation(const Vec3 &pos)
{
	*this = Matrix4x4::g_Identity;
	m[3][0] = pos.x;
	m[3][1] = pos.y;
	m[3][2] = pos.z;
}

void Matrix4x4::BuildTranslation(const float x, const float y, const float z )
{
	*this = Matrix4x4::g_Identity;
	m[3][0] = x;
	m[3][1] = y;
	m[3][2] = z;
}

inline Matrix4x4 operator * (const Matrix4x4 &a, const Matrix4x4 &b) 
{
	Matrix4x4 out;
	D3DXMatrixMultiply(&out, &a, &b);
	return out;
}